

cap log close 
clear all
set more off 
set niceness 10

gl MAIN = "/Users/Wei/Dropbox"
*gl MAIN = "C:\Users\huang\Dropbox"

gl path1 = "$MAIN/CFPS/cfps_official"
gl path2 = "$MAIN/Fertility/Workingdata"
gl path3 = "$MAIN/Fertility/Results"
gl path4 = "$MAIN/Fertility/Figures"
/*
cd "$path1/fulllaunch10"
unicode encoding set gb18030
unicode translate cfps2010adult_report.dta, invalid
unicode translate cfps2010family_report.dta, invalid
*/
log using "$path3/CFPS_data.log", replace 

use "$path1/fulllaunch10/cfps2010adult_report.dta", clear 
merge m:1 fid  using "$path1/fulllaunch10/cfps2010family_report.dta", keepusing(fincome faminc_net indinc_net) nogen
ren indinc_net fincome2_per
ren faminc_net fincome2
ren fincome finc

gen prov = qa102acode
replace prov = provcd if prov <=0 

gen sex = gender
gen year = 2010 
gen year_birth = qa1y_best if qa1y_best > 0 
gen smoke = qq2 == 1 
gen drink = qq3 == 1 
ren qk801 network
ren qk802 happiness 
ren qm403 life_sat 

gen age = year - year_birth 
gen sib = qb1

save "$path2/cfps_2010_temp",replace 
/*
clear 
cd "$path1/fulllaunch12"
unicode translate cfps2012adultcombined_072014.dta, invalid
unicode translate cfps2012famecon_072014.dta, invalid
use "$path1/fulllaunch12/cfps2012famecon_072014.dta", clear
*/
use "$path1/fulllaunch12/cfps2012adultcombined_072014.dta", clear 
merge m:1 fid12  using "$path1/fulllaunch12/cfps2012famecon_072014.dta", keepusing(fincome1 pce fincome2 fincome2_per_adj)  nogen
ren fincome2_per_adj fincome2_per
ren fincome1 finc
gen prov = qa401ccode


gen sex = cfps2012_gender_best
gen year = 2012 
gen year_birth = cfps2012_birthy_best if cfps2012_birthy_best > 0 
gen smoke = qq201 == 1 
gen drink = qq301 == 1 
ren qn12012 life_sat
ren qn12013 family_sat

gen age = year - year_birth 
save "$path2/cfps_2012_temp",replace 
/*
clear
cd "$path1/fulllaunch14"
unicode translate cfps2014_adult_20160601.dta, invalid
unicode translate cfps2014famecon_20160601.dta, invalid
*/
use "$path1/fulllaunch14/cfps2014famecon_20160601.dta", clear
use "$path1/fulllaunch14/cfps2014_adult_20160601.dta", clear 
merge m:1  fid14  using "$path1/fulllaunch14/cfps2014famecon_20160601.dta", keepusing(finc pce fincome2_per fincome2)  nogen
ren provcd14 provcd 
gen sex = cfps_gender
 gen year_birth = cfps_birthy
gen smoke = qq201 == 1 
gen drink = qq301 == 1 
ren qn12012 life_sat
ren qn12013 family_sat
gen year = 2014 
ren wordtest14 wordtest
ren mathtest14 mathtest
save "$path2/cfps_2014_temp",replace 

use "$path1/fulllaunch14/cfps2014famecon_20160601.dta", clear 

******* 
set more off
use "$path2/cfps_2010_temp", clear 
append using "$path2/cfps_2012_temp", force 
append using "$path2/cfps_2014_temp", force 
cap erase "$path2/cfps_2010_temp.dta"
cap erase "$path2/cfps_2012_temp.dta"
cap erase "$path2/cfps_2014_temp.dta"


drop  qb301_a_1- qb312_a_15 code_a_c1-td8_a_c10 qf201_s_1- jobc2lastdate_a_10
drop egc2012y_a_1- yincomea_b_1 egc2051_min_a_1- yincomea_b_1_new

egen gender_best = mean(sex), by(pid)
replace gender_best =  round(gender_best )
replace sex = gender_best 


egen birthy_best = mean(year_birth), by(pid)
replace year_birth =  round(birthy_best )


replace prov = . if prov < 0 
egen prov_best = mean(prov), by(pid)
replace prov = prov_best 
replace prov = provcd if mi(prov)
drop if prov <0 | mi(prov)

egen educ_best = mean(educ), by(pid)
replace educ = educ_best


gen women = sex == 0 


ren year yr_t
qui:{
forvalues age = 0(1)30{
gen year = year_birth + `age' 
merge m:1 year prov using "$path2/fines_use",keepusing(fine) nogen
replace fine = 0 if fine == . & year < 1979 
ren fine fine_age`age'
drop year
}
}

forvalues ag = 1(1)3{
g year = year_birth - `ag' 
merge m:1 prov year using "$path2/fines_use", keepusing(fine) nogen
replace fine = 0 if year < 1979
ren fine fine_f`ag'
drop year
}


ren yr_t year 
egen fine_birth = rowmean(fine_age0)
egen fine_1_9 = rowmean(fine_age1-fine_age9)
egen fine_10_19 = rowmean(fine_age10-fine_age19)
egen fine_20_25 = rowmean(fine_age20-fine_age25)
egen fine_0_10 = rowmean(fine_age0-fine_age10)
egen fine_11_20 = rowmean(fine_age11-fine_age20)
egen fine_21_25 = rowmean(fine_age21-fine_age25)


egen fine_0_6 = rowmean(fine_age0-fine_age6)
egen fine_7_12 = rowmean(fine_age7-fine_age12)
egen fine_13_18 = rowmean(fine_age13-fine_age18)
egen fine_19_25 = rowmean(fine_age19-fine_age25)

egen fine_8_15 = rowmean(fine_age8-fine_age15)

egen fine_f2_5 = rowmean(fine_f2 fine_f1 fine_age0 fine_age1-fine_age5)
egen fine_6_10 = rowmean(fine_age6-fine_age10)
egen fine_11_15 = rowmean(fine_age11-fine_age15)
egen fine_16_20 = rowmean(fine_age16-fine_age20)



keep if age >= 25 
keep if  year_birth >= 1940 & year_birth <= 1980
gl FINE_VAR = "fine_0_6 fine_7_12 fine_13_18 fine_19_25"
gl X_VAR = "fine_0_10 fine_11_20 fine_21_25"

replace life_sat = . if life_sat < 0 | life_sat > 10 
gen senior = educ >= 7 if !mi(educ)

gen local_status = qm402 
replace local_status = qn8012 if mi(local_status)


gen height = qp1 
replace height = qp101 if mi(height)
replace height = . if height < 100 | height > 200

gen hlth = qp3 
replace hlth = qp201 if mi(hlth)

replace happiness = qm2012 if year == 2014
replace happiness = . if happiness < 0 
replace happiness = happiness * 2 if year == 2010

cap drop confidence hav_conf
gen confidence = qn12014
replace confidence = qm404 if mi(confidence)
gen hav_conf =    confidence ==4 | confidence == 5  if !mi(confidence)
gen sat_life = life_sat ==4 | life_sat == 5 if !mi(life_sat)
gen sat_marr =    qm801 ==4 | qm801 == 5  if !mi(qm801) & qm801 >0 
gen sat_econ_other = qm802 == 4 | qm802 == 5  if !mi(qm802)
gen sat_duty_other =  qm803 ==  4| qm803 == 5  if !mi(qm803)
gen high_status =  local_status ==  4| local_status == 5  if !mi(local_status)
gen bad_gov =  qn1101 ==  4| qn1101 == 5  if !mi(qn1101)
gen healthy = (hlth ==1 & year == 2010) | (hlth <=3 & year >= 2012) if !mi(hlth)
gen imp_son_live = qm1003 == 5| qm1003 == 4  if !mi(qm1003)
gen imp_son_birth = qm1004 == 5| qm1004 == 4  if !mi(qm1003)
gen agree_women_marr =  qm1102 == 5 | qm1102 == 4    if !mi(qm1102)
gen agree_women_chid =  qm1103 == 5 | qm1103 == 4    if !mi(qm1103)

*replace hav_conf = . if confidence <=0 & confidence >10  | year < 2012 
replace sat_duty_other = . if year < 2012
gen men = 1-women


gen lndrowry = ln(qm9) if women == 1
replace lndrowry = . if women == 0 
replace lndrowry = ln(qm9) if women == 1
replace lndrowry =. if lndrowry < 3 & lndrowry >10
cap gen men = 1-women
egen fine_8_25 = rowmean(fine_age8-fine_age25)
egen fine_8_22 = rowmean(fine_age8-fine_age22)
egen fine_16_22 = rowmean(fine_age16-fine_age22)

egen fine_6_20 = rowmean(fine_age6-fine_age20)
egen fine_6_15 = rowmean(fine_age6-fine_age15)
gen happy = happiness >= 8 if !mi(happiness)

gen n_sib = qb1 if qb1 >=0 
replace mathtest = . if mathtest<0 
replace wordtest = . if wordtest<0 
pca mathtest wordtest
predict cognition
replace eduy = . if eduy >17
ren fincome2 finc_net 
ren fincome2_per finc_net_per

gen lnfinc = ln(finc)
gen lnfinc_per = ln(finc) - ln(finc_net/finc_net_per)
gen lnfinc_net = ln(finc_net)
gen lnfinc_net_per = ln(finc_net_per)
gen lnpce = ln(pce)

replace lnpce = . if lnpce < 7 | lnpce > 13
replace lnfinc_per = . if lnfinc_per < 5 | lnfinc_per > 13
replace lnfinc_net_per = . if lnfinc_net_per < 5 | lnfinc_net_per > 13
keep *id women men cog* *test senior eduy prov year_birth fine* n_sib year age happ* sat* agree* finc* pce lndrowry lnf* lnpce
replace age = year - year_birth
*compress 
save "$path2/CFPS_OCP",replace 

log close 
